/*
 *
 * System2 Simulation Framework
 * Copyright (C) 2011 - 2012 Jacob Dawid
 * jacob.dawid@googlemail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef ATOM_H
#define ATOM_H

#include <QThread>
#include "semaphorescopelocker.h"
#include "serializable.h"
#include "context.h"
#include "signalbundle.h"
#include "component.h"

namespace System2 {
    /**
      * \class Atom
      * \author Jacob Dawid <jacob.dawid@googlemail.com>
      * \date 12.01.2012
      */
    class Atom : public QThread, public Serializable {
        Q_OBJECT
    public:
        Atom(int id, Context *context, Component *component);
        ~Atom();

        enum State {
            Waiting,
            Running,
            Finished,
            Deposited
        };

        State state();

        QString serialize();
        bool unserialize(const QString& data);

        void execute();
        void deposit();

        void makeDependentOf(int atomId);
        bool isDependentOf(int atomId);
        bool isIndependent();

        void setSignalBundle(SignalBundle *signalBundle);
        SignalBundle *signalBundle();

        int id();

    signals:
        void deposited();

    protected slots:
        void run();

    private:
        int m_id;
        QList<int> m_dependencies;
        Context *m_context;
        Component *m_component;
        SignalBundle *m_signalBundle;

        State m_state;
        QSemaphore *m_stateSemaphore;
    };
}
#endif // ATOM_H
